package com.fita.wechat.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.fita.entity.data.City;
import com.fita.entity.data.Location;
import com.fita.wechat.dao.CityDao;
import com.fita.wechat.dao.LocationDao;
import com.fita.wechat.entity.search.CarLocationSearch;
import com.fita.wechat.service.LocationService;
import com.fita.wechat.utils.PinYinUtils;

/**
 * 
 * @author mohanlan
 *
 */
@Service("locationService")
public class LocationServiceImpl implements LocationService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Dao
    private LocationDao locationDao;
    
    @Dao
    private CityDao cityDao;
    
    
    /*
     * (non-Javadoc)
     * @see com.fita.wechat.service.LocationService#listAll()
     */
    @SuppressWarnings("unchecked")
    public List<com.fita.entity.data.Location> listAll() {
        return genericDaoSupport.searchForList("FROM Location WHERE disabled = false ORDER BY cityName ASC");
    }

    
    /*
     * (non-Javadoc)
     * @see com.fita.wechat.service.LocationService#list(java.lang.String, java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public List<com.fita.entity.data.Location> list(String country, String cityName, String supplier) {
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("country", country);
        params.put("cityName", cityName);
        params.put("supplier", supplier);
        
        return genericDaoSupport.searchForList("FROM Location WHERE disabled = false AND country = :country AND cityName = :cityName AND supplier = :supplier ORDER BY name ASC", params);
    }
    
    /*
     * (non-Javadoc)
     * @see com.fita.wechat.service.LocationService#load(java.lang.Long)
     */
    public Location load(Long locationId) {
        return genericDaoSupport.load(Location.class, locationId);
    }


	/* (non-Javadoc)
	 * @see com.fita.wechat.service.LocationService#search(com.fita.wechat.entity.search.CarLocationSearch, com.demo2do.core.entity.page.Page)
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> search(CarLocationSearch queryBuilder, Page page) {

		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		
		String sentence = queryBuilder.buildSentenceTemplate();
		
        Map<String, Object> params = queryBuilder.buildParameters();
        
        List<Location> locations = genericDaoSupport.searchForList(sentence, params, page.getBeginIndex(), page.getEveryPage());
        
        page.calculate();
		
        for (Location location : locations) {
			
        	Map<String, Object> locationMap = new HashMap<String, Object>();
        	
        	locationMap.put("id", location.getId());
        	
        	locationMap.put("name", location.getName());
        	
        	locationMap.put("cityName", location.getCityName());
        	
        	locationMap.put("country", location.getCountry());
        	
        	locationMap.put("isAirport", location.getAirportIndicator());
        	
        	locationMap.put("address1", location.getAddressOne());
        	
        	locationMap.put("address2", location.getAddressTwo());
        	
        	locationMap.put("phone", location.getPhone());
        		
        	maps.add(locationMap);
		}
		return maps;
	}

	/* (non-Javadoc)
	 * @see com.fita.wechat.service.LocationService#searchHotLocation(java.lang.String, java.lang.String)
	 */
	public Map<String, List<Map<String, Object>>> searchHotLocation(String country, String brand) {
		
		List<Map<String, Object>> locationMaps = new ArrayList<Map<String,Object>>();
		
		List<Location> hotLocations = null;
		
		if (country.contains(",")) {
			
			String[] countries = country.split(",");
			
			hotLocations = locationDao.listHotLocations(countries, brand);
		}else {
			hotLocations = locationDao.listHotLocations(country, brand);
		}
		for (Location location : hotLocations) {
			
			Map<String, Object> locationMap = new HashMap<String, Object>();
			
			locationMap.put("englishCityName", location.getCityName());
			
			City city = cityDao.loadByEnglishName(location.getCityName());
			
			locationMap.put("chineseCityName", city.getChineseName());
						
			locationMap.put("id", location.getId());
			
			locationMap.put("name", location.getName());
			
			locationMap.put("country", location.getCountry());
        	
        	locationMap.put("isAirport", location.getAirportIndicator());
        	
        	locationMap.put("firstPinYin", PinYinUtils.getFirstPinYin(city.getChineseName()));
        	
        	locationMap.put("address1", location.getAddressOne());
        	
        	locationMap.put("address2", location.getAddressTwo());
        	
        	locationMap.put("phone", location.getPhone());
        	
        	locationMaps.add(locationMap);
		}
		Map<String, List<Map<String, Object>>> collect = locationMaps.stream().collect(Collectors.groupingBy(new Function<Map<String, Object>, String>() {

			@Override
			public String apply(Map<String, Object> t) {
				
				return (String) t.get("firstPinYin");
			}
		}));
		return collect;
	}


	/* (non-Javadoc)
	 * @see com.fita.wechat.service.LocationService#listWorkingHours(java.lang.Long)
	 */
	public Map<String, String> listWorkingHours(Long locationId) {

		Location location = genericDaoSupport.load(Location.class, locationId);
		
		Map<String, String> workingHours = location.getWorkingHours();
		
		return workingHours;
	}

}
